package com.cloud.class05.controller;

import com.cloud.class05.entities.Pay;

import com.cloud.class05.entities.PayDTO;
import com.cloud.class05.resp.ResultData;
import com.cloud.class05.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2024/4/8 14:55
 */
@RestController
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping("/pay/add")
//    RequestBody的作用是将请求体中的json格式数据映射到Pay对象中
    @Operation(summary = "新增", description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        int add = payService.add(pay);
        return ResultData.success("成功插入记录，返回值：" + add);

    }

    //    @RequestMapping(value = "/pay/del/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/pay/del/{id}")
//    @PathVariable作用是将请求路径中的路径上参数映射到形参中
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<String> deletePay(@PathVariable("id") Integer id) {
        int delete = payService.delete(id);
        return ResultData.success("成功删除记录，返回值：" + delete);
    }

    @PutMapping("/pay/update")
//    @PathVariable作用是将请求路径中的路径上参数映射到形参中
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
//        copyProperties的作用是将payDTO中的属性值赋值给pay对象
        BeanUtils.copyProperties(payDTO, pay);
        int update = payService.update(pay);
        return ResultData.success("成功删除记录，返回值：" + update);

    }

    @GetMapping("/pay/get/{id}")
//    @PathVariable作用是将请求路径中的路径上参数映射到形参中
    @Operation(summary = "按照ID查流水", description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        try {
//
            Thread.sleep(62000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(1/0);
//        if(id == -4) throw new RuntimeException("id不能为负数");
        Pay pay = payService.getById(id);
        return ResultData.success(pay);

    }

    @GetMapping("/pay/getall")
//    @PathVariable作用是将请求路径中的路径上参数映射到形参中
    @Operation(summary = "查询所有流水信息", description = "查询支付所有流水方法")
    public ResultData<List<Pay>> getAll() {
//        System.out.println(1/0);
        List<Pay> all = payService.getAll();
        return ResultData.success(all);

    }

    @Value("${server.port}")
    private String port;

    @GetMapping("/pay/get/info")
    public String getPayInfoByConsul(@Value("${atclass.info}") String info) {
        return "端口号：" + port + "，读取到consul里面的信息为" + info;
    }
    @GetMapping("/pay/get/classin")
    public String getClassInfo(@Value("${class05.name}") String info) {
        return "读取到class05.name的值为：" + info + "，端口号为：" + port;
    }


}
